package com.mall.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {
    
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 扫描所有模块的 Controller
                .apis(RequestHandlerSelectors.basePackage("com.mall"))
                .paths(PathSelectors.any())
                .build()
                .groupName("mall-admin")
                .useDefaultResponseMessages(false);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Mall 商城系统接口文档")
                .description("Mall 商城系统后端接口文档，包含系统管理、商品管理等模块")
                .contact(new Contact("Mall Team", "http://localhost:8080", "admin@mall.com"))
                .version("1.0.0")
                .build();
    }
    
    // Swagger UI 相关的路径
    public static final String[] SWAGGER_WHITE_LIST = {
        "/doc.html",
        "/swagger-ui.html",
        "/swagger-ui/**",
        "/webjars/**",
        "/swagger-resources",
        "/swagger-resources/**",
        "/v2/api-docs",
        "/v3/api-docs",
        "/v3/api-docs/**",
        "/swagger-ui.html/**",
        "/favicon.ico",
        "/error"
    };
}
